<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">
        添加学科类型
      </el-button>
    </div>

    <el-table
      :data="list"
      border
      style="width: 100%"
    >
      <el-table-column
        label="ID"
        prop="id"
        width="200"
      />
      <el-table-column
        label="学科类型名称"
        prop="typeName"
      />
      <el-table-column
        label="创建时间"
        prop="createTime"
        width="180"
      />
      <el-table-column
        label="更新时间"
        prop="updateTime"
        width="180"
      />
      <el-table-column
        label="操作"
        align="center"
        width="200"
        fixed="right"
      >
        <template v-slot="{row}">
          <el-button size="mini" type="primary" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogTitle"
      width="40%"
    >
      <el-form
        ref="dataForm"
        :model="tempData"
        label-position="left"
        label-width="100px"
        style="margin-left: 20px"
      >
        <el-form-item label="学科类型名称" prop="typeName">
          <el-input v-model="tempData.typeName" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="saveData">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAll, saveData, deleteData, fetchDetail } from '@/api/repo/departType'

export default {
  name: 'DepartTypeList',
  data() {
    return {
      list: [],
      dialogVisible: false,
      dialogTitle: '',
      tempData: {
        id: undefined,
        typeName: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      listAll().then(response => {
        this.list = response.data
      })
    },

    handleCreate() {
      this.dialogTitle = '添加学科类型'
      this.tempData = {
        id: undefined,
        typeName: ''
      }
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },

    handleUpdate(row) {
      this.dialogTitle = '编辑学科类型'
      this.tempData = Object.assign({}, row)
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },

    saveData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveData(this.tempData).then(() => {
            this.$message({
              message: '保存成功',
              type: 'success'
            })
            this.dialogVisible = false
            this.getList()
          })
        }
      })
    },

    handleDelete(row) {
      this.$confirm('确认删除该学科类型吗？', '删除确认', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteData({ ids: [row.id] }).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getList()
        })
      })
    }
  }
}
</script>

<style scoped>

</style>